<template>
  <div class="orderManage">
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="add"> <PlusOutlined />新增 </a-button>
        <a-button type="primary" @click="edit"> <FormOutlined />编辑 </a-button>
        <a-button type="primary" @click="del"> <CloseOutlined />删除 </a-button>
        <a-button type="primary" @click="see"> <EyeOutlined />查看 </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, createVNode } from 'vue';
  import { useRouter } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable } from '/@/components/Table';
  import { getBasicColumns } from '/@/views/demo/table/tableData';
  import { demoListApi } from '/@/api/demo/table';
  import {
    PlusOutlined,
    FormOutlined,
    CloseOutlined,
    EyeOutlined,
    ExclamationCircleOutlined,
  } from '@ant-design/icons-vue';
  import { Modal } from 'ant-design-vue';
  export default defineComponent({
    components: { BasicTable, PlusOutlined, FormOutlined, CloseOutlined, EyeOutlined },
    setup() {
      inject: ['formConfigObj', 'a'];
      const router = useRouter();

      const [registerTable, { setLoading, getSelectRows, clearSelectedRowKeys }] = useTable({
        rowSelection: { type: 'checkbox' }, // 选择列配置
        showTableSetting: true, //表格设置工具
        api: demoListApi,
        columns: getBasicColumns(),
        formConfig: {
          labelWidth: 100,
          schemas: [
            {
              field: `orderCode`,
              label: `订单编号`,
              component: 'Input',
              colProps: {
                xl: 6,
                xxl: 5,
              },
            },
            {
              field: `orderDate`,
              label: `订单日期`,
              component: 'DatePicker',
              colProps: {
                xl: 6,
                xxl: 5,
              },
            },
            {
              field: `orderType`,
              label: `订单类型`,
              component: 'Select',
              colProps: {
                xl: 6,
                xxl: 5,
              },
              componentProps: {
                options: [
                  {
                    lable: '选择1',
                    value: '1',
                  },
                  {
                    lable: '选择2',
                    value: '2',
                  },
                ],
              },
            },
          ],
        },
        useSearchForm: true, //使用搜索表单
      });

      const { createMessage } = useMessage();
      const { warning, success } = createMessage;

      function changeLoading() {
        setLoading(true);
        setTimeout(() => {
          setLoading(false);
        }, 1000);
      }
      function add(): void {
        router.push({
          path: '/logisticsManagement/orderManage/newOrder',
        });
      }

      function edit(): void {
        const slectRow = getSelectRows();
        if (Object.keys(slectRow).length == 0) {
          warning('请选择想要编辑的记录');
          return;
        } else if (Object.keys(slectRow).length > 1) {
          warning('只能选择一条');
          return;
        }
      }

      function del(): void {
        const slectRow = getSelectRows();
        if (Object.keys(slectRow).length == 0) {
          warning('请选择想要删除的记录');
          return;
        } else {
          Modal.confirm({
            title: '删除',
            icon: createVNode(ExclamationCircleOutlined),
            content: '您确定要删除选中的记录吗?',
            onOk() {
              success('删除成功!');
              clearSelectedRowKeys();
            },
            onCancel() {
              clearSelectedRowKeys();
            },
            class: 'test',
          });
        }
      }
      function see(): void {
        const slectRow = getSelectRows();
        if (Object.keys(slectRow).length == 0) {
          warning('请选择任意一条记录');
          return;
        } else if (Object.keys(slectRow).length > 1) {
          warning('只能选择一条');
          return;
        }
      }

      return {
        registerTable,
        changeLoading,
        edit,
        add,
        del,
        see,
      };
    },
  });
</script>
<style lang="less" scoped>
  /deep/button {
    margin-right: 5px;
  }
</style>
